package a.student.dao;

import java.util.ArrayList;

import a.student.library.*;
import a.student.utils.aStudentApplication;
import a.student.helper.DatabaseHelper;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class SemesterDAO extends BaseDao<Semester>{

	public SemesterDAO(Context context, String databaseName) {
		super(context, databaseName);
	}
	
	public SemesterDAO(SQLiteDatabase db) {
		super(db);
	}
	
	public SemesterDAO(DatabaseHelper helper) {
		super(helper);
	}
	
//	public SemesterDAO(String databaseName) {
//		aStudentApplication application = 
//	}

	public long addNewSemester(Semester semesterInfo){
		
		ContentValues cv=new ContentValues();
		cv.put(Semester.SEMESTER_SNAME, semesterInfo.semesterName);
		cv.put(Semester.SEMESTER_DATESTART, semesterInfo.dateStart);
		cv.put(Semester.SEMESTER_DATEEND, semesterInfo.dateEnd);
		
		long result = super.insert(Semester.SEMESTER_TBL_NAME, cv);
		if(result > 0)
			semesterInfo.setSemesterId((int)result);
		return result;
		
	}

	public long removeSemester(Semester semesterInfo){
		ContentValues cv = new ContentValues();
		cv.put(Semester.SEMESTER_ID, semesterInfo.getSemesterId());
		long count = super.delete(Semester.SEMESTER_TBL_NAME, cv);
		return count;
		
	}
    
    public ArrayList<Semester> getAllSemester(){
    	Cursor c = super.getAll(Semester.SEMESTER_TBL_NAME, Semester.SEMESTER_DEFAULT_SORT_ORDER);
    	ArrayList<Semester> array = new ArrayList<Semester>();
    	if(c==null)
    		return array;
    	while(!c.isAfterLast()){
    		array.add(new Semester(c));
    		c.moveToNext();
    	}
    	c.close();
    	return array;
    }
    
    public Semester findSemester(final int semesterID) {
        ContentValues cv = new ContentValues();
        cv.put(Semester.SEMESTER_ID, semesterID);
        Cursor c = super.find(Semester.SEMESTER_TBL_NAME, cv,Semester.SEMESTER_DEFAULT_SORT_ORDER);
        Semester semester = null;
        if (c != null) {
            try {
            	semester = new Semester(c);
            } catch (Exception e) {
            	e.printStackTrace();
			}
            finally {
                c.close();
            }
        }
        return semester;
    }
    
	public boolean updateSemesterDetail(Semester semesterInfo){
		
		boolean flag = true;
		ContentValues cv=new ContentValues();
		cv.put(Semester.SEMESTER_SNAME, semesterInfo.semesterName);
		cv.put(Semester.SEMESTER_DATESTART, semesterInfo.dateStart);
		cv.put(Semester.SEMESTER_DATEEND, semesterInfo.dateEnd);
		flag = super.update(Semester.SEMESTER_TBL_NAME, cv, Semester.SEMESTER_ID + "=?", new String[]{String.valueOf(semesterInfo.getSemesterId())})>0;
    	return flag;
	}


}
